package com.app.micaihu.view.main.mine.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.app.micaihu.R;
import com.app.micaihu.adapter.NewsAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.db.NewsDBUtil;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.videoplayer.JCVideoPlayer;
import com.app.utils.pulltorefresh.PullToRefreshBase;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.ActivityUtils;

import java.util.HashMap;

/**
 * Created by tech-111 on 2018/10/10.
 */
public class BrowseHistoryActivity extends BaseListActivity<NewsEntity> {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("阅读历史");
        mListView.setMode(PullToRefreshBase.Mode.DISABLED);
        dataList = NewsDBUtil.getInstance().getHistoryData();
        if (dataList.size() > 0) {
            setTitleRightText("清空").setOnClickListener(this);
            mAdapter = new NewsAdapter(dataList, this, TypeConstant.CHANNEL_TYPE_NEWS, "", null, "");
            mListView.setAdapter(mAdapter);
            setLoadView(LOADSUCES, "");
        } else {
            setLoadView(DATAEMPTY, R.drawable.empty_icon_collect, getString(R.string.pullup_no_data));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //暂停视频
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void changeData(boolean isRefresh) {
    }


    // 设置listview的最后一行显示的监听事件
    public void setListViewOnLastItemVisibleListener() {
        // 本类中这句代码AppViewUtils.initPullToRefreshListView(getActivity(),
        // mListView);
        // 已经设置了listview自动加载下一页了
        // 如果不想listview自动加载下一页就重写本方法设置listview最后一行显示的监听为null
        mListView.setOnLastItemVisibleListener(null);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        NewsEntity news = (NewsEntity) mAdapter.getItem(position - 1);
        if (!TypeConstant.openTypeDetail(news, this, TypeConstant.SOURCETYPE_NORMAL)) {

            if (news == null) {
                return;
            }
            if (TextUtils.equals(news.getArticleType(), TypeConstant.FLUSH)) {
                flushData();
                StatService.onEvent(this, "032", TypeConstant.CHANNEL_TYPE_NEWS, 1);// 统计的点击
            }
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.page_head_function_text:
                HashMap<String, String> style = new HashMap<>();
                style.put("title", AppApplication.getInstance().getString(R.string.common_prompt));
                style.put("desc", "确定要删除吗");
                style.put("btnPosi", AppApplication.getInstance().getString(R.string.common_confirm));
                style.put("btnNega", AppApplication.getInstance().getString(R.string.common_cancel));

                DialogUtils.getInstance().setOnDialogBtnClick(new DialogUtils.OnDialogBtnClickListener() {
                    @Override
                    public void onPositiveClick() {
                        NewsDBUtil.getInstance().deleteHistoryDate();
                        dataList.clear();
                        mAdapter.resetList(dataList);
                        mAdapter.notifyDataSetChanged();
                        setTitleRightText("");
                        setLoadView(DATAEMPTY, R.drawable.empty_icon_collect, getString(R.string.pullup_no_data));
                    }

                    @Override
                    public void onNegativeClick() {

                    }
                });
                DialogUtils.getInstance().showCustomDialog(ActivityUtils.getTopActivity(), style);
                break;
        }
    }
}
